<?php
namespace app\api\controller;

/**
 * 
 * 
 * */
class CompanyIncomeExpense extends Permission
{
    public function getList()
    {
        $page = $this->post('page') ? $this->post('page') : 1;
        $limit = $this->post('limit') ? $this->post('limit') : 15;
        
        $map = $this->_listMap();
        
        $total = db('CompanyIncomeExpense')->where($map)->count();

        $lists = db('CompanyIncomeExpense')->where($map)->order('id DESC')->page($page,$limit)->select();
        
        $sql = db()->getLastSql();

        $list = $this->_formatList($lists);


        $data = array(
            'total' => $total,
            'list' => $list,
            'sql' => $sql,
            'map' => $map
        );
        $this->resSuc($data);
    }

    private function _listMap()
    {
        $map = [];

        if($this->post('belongs_date') != '') $map['belongs_date'] = $this->post('belongs_date');

        if($this->post('belongs_month') != '') $map['belongs_date'] = ['LIKE','%'.$this->post('belongs_month').'%'];


        if($this->post('site_id') != '') $map = $this->setMaps($map,'site_id');
        if($this->post('status') != '') $map = $this->setMaps($map,'status');
        if($this->post('company_id') != '') $map = $this->setMaps($map,'company_id');
        if($this->post('company') != '') $map = $this->setMaps($map,'company','LIKE');
        if($this->post('position') != '') $map = $this->setMaps($map,'position','LIKE');

        return $map;
    }
    private function _getBadgeData($map)
    {
        $badgeData = [];
        $judgeConfig = db('CompanyIncomeExpenseJudgeConfig')->where(['tenant_id'=>$this->user['tenant_id']])->select();

        $judgeConfig[] = ['sort' => 0];
        $judgeConfig[] = ['sort' => 190];
        foreach ($judgeConfig as $v) {
            $status = $v['sort'];
            $map['status'] = $status;
            $count = db('CompanyIncomeExpense')->where($map)->count();
            $badgeData[$status] = $count;
        }


        return $badgeData;

    }
    private function _formatList($data)
    {
        $list = [];
        foreach ($data as $v) {
            $v['area_id'] = $v['area_id'] == 0 ? '' : $v['area_id'];
            $v['company_id'] = $v['company_id'] == 0 ? '' : $v['company_id'];
            $v['imageList'] = json_decode($v['images'],true);
            $v['amount'] = formatMoneyShow($v['amount']);
            $v['belongs_date'] = date('Y-m-d',$v['belongs_date']);
            $v['create_time'] = date('Y-m-d',$v['create_time']);
            $v['judge_time'] = date('m-d H:i',$v['judge_time']);
            $v['reject_time'] = date('m-d H:i',$v['reject_time']);
            $v['statusText'] = $v['status'] == 0 ? '待审核' : '已审核';
            $v['statusType'] = $v['status'] == 0 ? 'info' : 'success';
            $list[] = $v;
        }

        return $list;
    }


    public function del()
    {
        $map['id'] = $this->post('id');
        db('CompanyIncomeExpense')->where($map)->delete();
        $this->resSuc('success');
    }

    public function update()
    {
        $data['id'] = $this->post('id');
        $data['io_type'] = $this->post('io_type');
        $data['belongs'] = $this->post('belongs');
        $data['belongs_date'] = strtotime( $this->post('belongs_date') );

        $data['cate_id'] = $this->post('cate_id');
        $data['cate'] = db('CompanyIncomeExpenseCate')->where(['id'=>$this->post('cate_id')])->value('title');

        $companyId = $this->post('company_id');
        $data['company_id'] = $companyId;
        $data['company'] = $companyId > 0 ? db('Company')->where(['id'=>$companyId])->value('title') : '';

        $areaId = $this->post('area_id');
        $data['area_id'] = $areaId;
        $data['area'] = $areaId > 0 ? db('CompanyArea')->where(['id'=>$areaId])->value('title') : '';

        

        $data['amount'] = formatMoneySave($this->post('amount'));
        $data['remark'] = $this->post('remark');
        $data['images'] = $this->post('images');

        $cateId = $this->post('cate_id');


  
        if($data['id'] > 0)
        {
            if($this->post('status') == 500) $data['status'] = 0; // 重新提交
            db('CompanyIncomeExpense')->update($data);
        }
        else
        {
            unset($data['id']);
            $data['create_time'] = time();
            $data['create_user'] = $this->user['username'];
            $data['create_user_id'] = $this->user['id'];
            $id = db('CompanyIncomeExpense')->insertGetId($data);
        }

        $this->resSuc('ok');
    }


    public function getById()
    {
        $map = [
            'id' => $this->post('id')
        ];
        $CompanyIncomeExpense = db('CompanyIncomeExpense')->where($map)->find();

        $this->resSuc($CompanyIncomeExpense);
    }

    public function pass()
    {
        $ids = json_decode($this->post('ids'));

        $map = [
            'id' => ['IN',$ids]
        ];
        $data = [
            'status' => 200,
            'judge_user' => $this->user['realname'],
            'judge_user_id' => $this->user['id'],
            'judge_time' => time()
        ];
        db('CompanyIncomeExpense')->where($map)->update($data);

        $this->resSuc('ok');
    }

    public function reject()
    {
        $ids = json_decode($this->post('ids'));

        $map = [
            'id' => ['IN',$ids]
        ];
        $data = [
            'status' => 500,
            'reject_reason' => $this->post('reject_reason'),
            'reject_user' => $this->user['realname'],
            'reject_user_id' => $this->user['id'],
            'reject_time' => time()
        ];
        db('CompanyIncomeExpense')->where($map)->update($data);

        $this->resSuc('ok');
    }

    public function setStatus()
    {
        $id = $this->post('id');
        $status = $this->post('status');
        $map = [
            'id' => $id
        ];

        if($status == 500)
        {
            # 拒绝
            $data = [
                'status' => $status,
                'reject_user' => $this->user['realname'],
                'reject_user_id' => $this->user['id'],
                'reject_time' => time(),
            ];
        }
        if($status == 200)
        {
            # 已通过已审核
            $data = [
                'status' => $status,
                'done_user' => $this->user['realname'],
                'done_user_id' => $this->user['id'],
                'done_time' => time(),
            ];
        }
        
        db('CompanyIncomeExpense')->where($map)->update($data);
        $this->resSuc('ok');
    }

    private function _updateJudgeTimes($id)
    {
        $map = [
            'cost_id' => $id
        ];
        $count = db('CompanyIncomeExpenseJudgeLogs')->where($map)->count();

        $map = [
            'id' => $id
        ];
        $data = [
            'judge_times' => $count
        ];
        db('CompanyIncomeExpense')->where($map)->update($data);
    }


    public function add()
    {
        $list = json_decode($this->post('costList'),true);
        foreach ($list as $v) {
            if($v['amount'] == '' || $v['amount'] == 0) continue;
            $data = [
                'tenant_id' => $this->post('tenant_id'),
                'site_id' => $this->post('site_id'),
                'company_id' => $this->post('company_id'),
                'company' => $this->post('company'),
                'position' => $this->post('position'),
                'position_id' => $this->post('position_id'),
                'belongs_date' => $this->post('belongs_date').'-01',
                'cate' => $v['cate'],
                'amount' => formatMoneySave($v['amount']),
                'remark' => $v['remark'],
                'images' => $v['images'],
                'create_user' => $this->user['realname'],
                'create_user_id' => $this->user['id'],
                'create_time' => time(),
            ];
            db('CompanyIncomeExpense')->insert($data);
        }
        $this->resSuc('添加成功');
    }

}
